package pub.tbc.api.invoker.router;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.beans.XMLDecoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
public class RouteTable {

    private static List<RouterInfo> routerInfoList = new ArrayList<>();

    private static Map<String, String> routerTable = Maps.newHashMap();

    private static RouteTable instance = null;

    private String oauthAddress;

    public final static String ROUTER_TABLE_FILE = "routerTable.xml";

    /**
     * 不考虑线程安全，只在系统启动时自己调用
     *
     * @return 实例
     * @throws FileNotFoundException .
     */
    public static RouteTable getInstance() throws FileNotFoundException {
        if (instance == null) {
            File routerTableConfigFile = new File(ROUTER_TABLE_FILE);
            XMLDecoder decoder = new XMLDecoder(new FileInputStream(routerTableConfigFile));
            instance = (RouteTable) decoder.readObject();
        }
        return instance;
    }

    public String getServerName(String uri) {
        return routerTable.get(uri);
    }


}